package com.example.segment_saver;
/*
 * Nov 16th 2012
 * Created By: Alex Drotar, Jacob Reiger 
 * Using code from: Ken Anderson 
 * Segment_Saver- This is meant to be run on Andriod mobile devices
 * 					The app will allow the user to take in a url that they
 * 					want to watch. If the user selects the update site button
 * 					The application will go and check if the site has changed 
 * 					since the last time the user looked at it.
 * 
 */

import java.io.File;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.app.ListFragment;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SiteDetailFragment  extends Fragment{

	
	private SiteList sites = new SiteList();
	private String Url;
	private boolean saveData = true;
	
	@Override
	public void onCreate (Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
		Url = getActivity().getIntent().getStringExtra("Url"); 
	}
	//@Override
	public void onResume()
	{
		super.onResume();
		setHasOptionsMenu(true);
		saveData = false;
		sites.load(new File(getActivity().getFilesDir(), "sites.bin"));
		
		if(Url != null)
		{
			//Log.i("Segment Saver", "This is a Test:  " + Url);

			if(Url != "")
			{
				SiteData data = sites.getSite(Url);
			
				EditText site = (EditText) getActivity().findViewById(R.id.Url_name);
				
				site.setText(data.getUrl());	
			}
					
		}
	}
	
	@Override
	public void onPause()
	{
		super.onPause();
		setHasOptionsMenu(true);
		sites.load(new File(getActivity().getFilesDir(), "sites.bin"));

			
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
	{
		return inflater.inflate(R.layout.fragment_add_site, container, false);
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		inflater.inflate(R.menu.fragment_delete_site, menu);
	}
	
	@Override
	public boolean onOptionsItemSelected (MenuItem item)
	{
		if(item.getItemId() == R.id.menu_add_site)
		{
			//Log.i("Segment Saver", "This is a test... It should have recieved the add_site");
			SiteData data = null;

			EditText site = (EditText) getActivity().findViewById(R.id.Url_name);
			
			String site_url = site.getText().toString();
					
			Url = site_url;
			if(Url!=null)
			{
				data = new SiteData(site_url);
				
				sites.addSite(Url, data);
				//Log.i("Segment Saver", "Trying to add URL" + Url+"ADA");
				sites.save(new File(getActivity().getFilesDir(), "sites.bin"));
				
			}
			site.setText("");
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
		//sites.load(new File(getActivity().getFilesDir(), "sites.bin"));

		/*
		if(item.getItemId() == R.id.menu_delete_site)
		{
			Log.i("Segment Saver", "Delete Button Selected Item number: SiteDetailFrag");

		}*/
		
		getActivity().recreate();
		//getActivity().finish();
		return true;
	}
	
	
	
}
